package saint.UI.map;

import android.graphics.*;
import com.google.android.maps.*;



public class RouteOverlay extends Overlay  {
	private GeoPoint mGeoPoint;
	private Paint routePaint;
	private GeoPoint routePoints[];
	private int routeGrade[];
	private boolean routeIsActive;
	private Point pOld, pNew, pP;
	private int numberOfRoutePoint;
	
	//============ Class Constructor ========================================//
	/**
	 * Class constructor
	 * @param routePoint Point of location in the earth.
	 * @param routeGrade 
	 */
	public RouteOverlay(GeoPoint[] routePoint, int[] routeGrade) {
		this.routePoints = routePoint;
		this.routeGrade = routeGrade;
		numberOfRoutePoint = routePoints.length;
		routeIsActive = true;
		mGeoPoint = routePoints[0];
		pOld = new Point(0,0);
		pNew = new Point(0,0);
		pP = new Point(0,0);
		routePaint = new Paint();
	}
	//============ Public method ============================================//
	public void setRouteView(boolean routeIsActive) {
		this.routeIsActive = routeIsActive;
	}
	
	//============ Override method ==========================================//
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		int xOff = 0;
		int yOff = 0;
		int oldX;
		int oldY;
		int newX;
		int newY;
		
		super.draw(canvas, mapView, shadow);
		if(!routeIsActive) { return; }
		
		mapView.getProjection().toPixels(mGeoPoint, pP);  // Convert GeoPoint to screen pixels
		oldX = pP.x;
		oldY = pP.y;
		newX = oldX + xOff;
		newY = oldY + yOff;
		
		routePaint.setAntiAlias(true);
		
		//Draw route
		for(int i = 0; i < numberOfRoutePoint - 1; i++) {
			switch (routeGrade[i]) {
				case 1 : 
					routePaint.setARGB(100, 255, 0, 0);		// Red color and opacity 40%
					routePaint.setStrokeWidth(3);
					break;
				case 2 :
					routePaint.setARGB(100, 0, 255, 0); 	// Blue color and opacity 40%
					routePaint.setStrokeWidth(5);
					break;
				case 3 :
					routePaint.setARGB(100, 0, 0, 255); 	// Green color and opacity 40%
					routePaint.setStrokeWidth(6);
					break;
				case 4 :
					routePaint.setARGB(90, 125, 102, 153); 	// 
					routePaint.setStrokeWidth(6);
					break;
			}
			// Find end point of this segment in pixels
			mapView.getProjection().toPixels(routePoints[i], pOld);
			oldX = pOld.x;
			oldY = pOld.y;
			mapView.getProjection().toPixels(routePoints[i + 1], pNew);
			newX = pNew.x;
			newY = pNew.y;
			
			// Draw line
			canvas.drawLine(oldX, oldY, newX, newY, routePaint);
		}
	}
}
